home *** CD-ROM | disk | FTP | other *** search
/ Mac Format 1997 July / macformat52.iso / mac / Shareware Plus / Developers / YAAF v1.0 alpha 1 / (Sources) / Standard Controls / Lists / XGStdListBox.cpp < prev   
Text File  |  1997-04-24  |  9KB  |  466 lines

  1. /*    XGStdListBox.cpp
  2.  *
  3.  *        Create a standard list box
  4.  */
  5.  
  6. /*  YAAF - Yet another application framework
  7.  *  Copyright (C) 1997 William Edward Woody and In Phase Consulting
  8.  *  
  9.  *  This library is free software; you can redistribute it
  10.  *  and/or modify it under the terms of the GNU Library
  11.  *  General Public License as published by the Free Software
  12.  *  Foundation; either version 2 of the License, or any
  13.  *  later version.
  14.  *  
  15.  *  This library is distributed in the hope that it will be
  16.  *  useful, but WITHOUT ANY WARRANTY; without even the implied
  17.  *  warranty of MERCHANTABIILITY or FITNESS FOR A PARTICULAR
  18.  *  PURPOSE. See the GNU Library General Public License for
  19.  *  more details.
  20.  *  
  21.  *  You should have received a copy of the GNU Library General
  22.  *  Public License along with this library; if not, write to the
  23.  *  Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  24.  *  Boston, MA 02111-1307, USA.
  25.  *  
  26.  *  To contact the author, either e-mail me at
  27.  *  woody@alumni.caltech.edu, or write to us at
  28.  *  
  29.  *          William Edward Woody
  30.  *          In Phase Consulting
  31.  *          1545 Ard Eevin Avenue
  32.  *          Glendale, CA 91202
  33.  */
  34.  
  35. #include <string.h>
  36. #include <XApplication.h>
  37. #include <XStdControls.h>
  38. #include <XDataUtil.h>
  39. #include <XError.h>
  40. #include <XWindow.h>
  41. #include <XStdList.h>
  42.  
  43. /************************************************************************/
  44. /*                                                                        */
  45. /*    Construction/Destruction                                            */
  46. /*                                                                        */
  47. /************************************************************************/
  48.  
  49. /*    XGStdListBox::XGStdListBox
  50.  *
  51.  *        Create a standard list box
  52.  */
  53.  
  54. XGStdListBox::XGStdListBox(XGView *parent, XGArgStream &init) :
  55.     XGStdList(parent,init,true)
  56. {
  57.     Init(init.GetBoolean());
  58. }
  59.  
  60. XGStdListBox::XGStdListBox(XGView *parent, XGSListInitRecord &init) :
  61.     XGStdList(parent,init.v,true)
  62. {
  63.     Init(init.msel);
  64. }
  65.  
  66. /*    XGStdListBox::~XGStdListBox
  67.  *
  68.  *        Delete me
  69.  */
  70.  
  71. XGStdListBox::~XGStdListBox()
  72. {
  73. #if OPT_MACOS == 1
  74.     if (fList) LDispose(fList);
  75. #endif
  76. }
  77.  
  78. /************************************************************************/
  79. /*                                                                        */
  80. /*    Content modification                                                */
  81. /*                                                                        */
  82. /************************************************************************/
  83.  
  84. /*    XGStdListBox::Length
  85.  *
  86.  *        Get the length of this thing
  87.  */
  88.  
  89. long XGStdListBox::Length() const
  90. {
  91. #if OPT_MACOS == 1
  92.     return (**fList).dataBounds.bottom - (**fList).dataBounds.top;
  93. #endif
  94. }
  95.  
  96. /*    XGStdListBox::Insert
  97.  *
  98.  *        Insert a new entry
  99.  */
  100.  
  101. void XGStdListBox::Insert(long pos, const char *c)
  102. {
  103. #if OPT_MACOS == 1
  104.     Point p;
  105.     
  106.     InvalView();
  107.     ::LSetDrawingMode(false,fList);
  108.     ::LAddRow(1,pos,fList);
  109.     p.h = 0;
  110.     p.v = pos;
  111.     ::LSetCell((Ptr)c,strlen(c),p,fList);
  112.     ::LSetDrawingMode(true,fList);
  113. #endif
  114. }
  115.  
  116. /*    XGStdListBox::Delete
  117.  *
  118.  *        Delete an entry
  119.  */
  120.  
  121. void XGStdListBox::Delete(long pos)
  122. {
  123. #if OPT_MACOS == 1
  124.     InvalView();
  125.     ::LSetDrawingMode(false,fList);
  126.     ::LDelRow(1,pos,fList);
  127.     ::LSetDrawingMode(true,fList);
  128. #endif
  129. }
  130.  
  131. /*    XGStdListBox::Get
  132.  *
  133.  *        Get the contents of this row
  134.  */
  135.  
  136. void XGStdListBox::Get(long pos, char *c) const
  137. {
  138. #if OPT_MACOS == 1
  139.     short len;
  140.     Point p;
  141.     
  142.     p.h = 0;
  143.     p.v = pos;
  144.     ::LGetCell((Ptr)c,&len,p,fList);
  145.     c[len] = 0;
  146. #endif
  147. }
  148.  
  149. /*    XGStdListBox::Set
  150.  *
  151.  *        Set the contents of this thing
  152.  */
  153.  
  154. void XGStdListBox::Set(long pos, const char *c)
  155. {
  156. #if OPT_MACOS == 1
  157.     Point p;
  158.     
  159.     InvalView();
  160.     ::LSetDrawingMode(false,fList);
  161.     p.h = 0;
  162.     p.v = pos;
  163.     ::LSetCell((Ptr)c,strlen(c),p,fList);
  164.     ::LSetDrawingMode(true,fList);
  165. #endif
  166. }
  167.  
  168. /************************************************************************/
  169. /*                                                                        */
  170. /*    Selection Routines                                                    */
  171. /*                                                                        */
  172. /************************************************************************/
  173.  
  174. /*    XGStdListBox::GetSelect
  175.  *
  176.  *        Get the first selected item.
  177.  */
  178.  
  179. long XGStdListBox::GetSelect(void) const
  180. {
  181. #if OPT_MACOS == 1
  182.     Point pt;
  183.     
  184.     pt.h = 0;
  185.     pt.v = 0;
  186.     if (!::LGetSelect(true,&pt,fList)) return -1;
  187.     return pt.v;
  188. #endif
  189. }
  190.  
  191. /*    XGStdListBox::IsSelect
  192.  *
  193.  *        Is this selected?
  194.  */
  195.  
  196. bool XGStdListBox::IsSelect(long l) const
  197. {
  198. #if OPT_MACOS == 1
  199.     Point pt;
  200.     
  201.     pt.h = 0;
  202.     pt.v = l;
  203.     if (!::LGetSelect(false,&pt,fList)) return false;
  204.     return true;
  205. #endif
  206. }
  207.  
  208.  
  209. /*    XGStdListBox::SetSelect
  210.  *
  211.  *        Set the selected state. If this is a single select cell, this
  212.  *    will clear the other cells.
  213.  */
  214.  
  215. void XGStdListBox::SetSelect(long l, bool s)
  216. {
  217. #if OPT_MACOS == 1
  218.     XGDraw draw(this,false);
  219.     ::TextFont(fFont);
  220.     ::TextSize(fSize);
  221.  
  222.     Point pt;
  223.     long i;
  224.     
  225.     if (fMultiSelect && s) {
  226.         for (i = Length() - 1; i >= 0; i--) {
  227.             pt.h = 0;
  228.             pt.v = i;
  229.             ::LSetSelect((l == i) ? true : false, pt, fList);
  230.         }
  231.     } else {
  232.         pt.h = 0;
  233.         pt.v = l;
  234.         ::LSetSelect(s,pt,fList);
  235.     }
  236. #endif
  237. }
  238.  
  239. /*    XGStdListBox::ClearSelect
  240.  *
  241.  *        Clear the selected state.
  242.  */
  243.  
  244. void XGStdListBox::ClearSelect(void)
  245. {
  246. #if OPT_MACOS == 1
  247.     XGDraw draw(this,false);
  248.     ::TextFont(fFont);
  249.     ::TextSize(fSize);
  250.  
  251.     Point pt;
  252.     long i;
  253.     
  254.     for (i = Length() - 1; i >= 0; i--) {
  255.         pt.h = 0;
  256.         pt.v = i;
  257.         ::LSetSelect(false, pt, fList);
  258.     }
  259. #endif
  260. }
  261.  
  262. /************************************************************************/
  263. /*                                                                        */
  264. /*    Events                                                                */
  265. /*                                                                        */
  266. /************************************************************************/
  267.  
  268. /*    XGStdListBox::DoDrawView
  269.  *
  270.  *        Draw this thing
  271.  */
  272.  
  273. void XGStdListBox::DoDrawView(Rect r)
  274. {
  275. #if OPT_MACOS == 1
  276.     Rect s;
  277.     RgnHandle rgn;
  278.     
  279.     {
  280.         XGDraw draw(this,false);
  281.         ::TextFont(fFont);
  282.         ::TextSize(fSize);
  283.     
  284.         rgn = ::NewRgn();
  285.         ViewToGlobal(&r);
  286.         ::RectRgn(rgn,&r);
  287.     
  288.         ::LSetDrawingMode(true,fList);
  289.         ::LUpdate(rgn,fList);
  290.     
  291.         ::DisposeRgn(rgn);
  292.     }
  293.     
  294.     {
  295.         XGDraw draw(this);
  296.         
  297.         s = GetContentRect();
  298.         ::FrameRect(&s);
  299.     }
  300. #endif
  301. }
  302.  
  303. /*    XGStdListBox::DoActivate
  304.  *
  305.  *        Do activation/deactivation
  306.  */
  307.  
  308. void XGStdListBox::DoActivate(bool b)
  309. {
  310. #if OPT_MACOS == 1
  311.     XGDraw draw(this,false);
  312.     ::TextFont(fFont);
  313.     ::TextSize(fSize);
  314.     
  315.     ::LActivate(b,fList);
  316. #endif
  317. }
  318.  
  319. /*    XGStdListBox::DoMouseDown
  320.  *
  321.  *        Handle mouse down event
  322.  */
  323.  
  324. bool XGStdListBox::DoMouseDown(Point pt, short)
  325. {
  326. #if OPT_MACOS == 1
  327.     bool res;
  328.     
  329.     {
  330.         XGDraw draw(this,false);
  331.         ::TextFont(fFont);
  332.         ::TextSize(fSize);
  333.  
  334.         ViewToGlobal(&pt);
  335.         res = ::LClick(pt,_GCurEvent.modifiers,fList);
  336.     }
  337.     if (res) {
  338.         ReceiveDispatch(KEventListDblClk,GetViewID(),(void *)this);
  339.     } else {
  340.         ReceiveDispatch(KEventListSelect,GetViewID(),(void *)this);
  341.     }
  342. #endif
  343.  
  344.     return false;
  345. }
  346.  
  347. /*    XGStdListBox::DoSizeView
  348.  *
  349.  *        Do the size view
  350.  */
  351.  
  352. void XGStdListBox::DoSizeView()
  353. {
  354. #if OPT_MACOS == 1
  355.     {
  356.         Rect r;
  357.         Point size;
  358.         FontInfo finfo;
  359.         XGDraw draw(this,false);
  360.         ::TextFont(fFont);
  361.         ::TextSize(fSize);
  362.         
  363.         r = GetContentRect();
  364.         ::InsetRect(&r,1,1);
  365.         ::LSize(r.right-r.left-15,r.bottom-r.top,fList);
  366.  
  367.         size.h = r.right - r.left;
  368.         GetFontInfo(&finfo);
  369.         size.v = finfo.ascent + finfo.descent + finfo.leading;
  370.         ::LCellSize(size,fList);
  371.     }
  372.     InvalView();
  373. #endif
  374. }
  375.  
  376.  
  377. /*    XGStdListBox::DoMoveView
  378.  *
  379.  *        Move to a new location
  380.  */
  381.  
  382. void XGStdListBox::DoMoveView()
  383. {
  384. #if OPT_MACOS == 1
  385.     Rect r;
  386.     XGDraw draw(this,false);
  387.     ::TextFont(fFont);
  388.     ::TextSize(fSize);
  389.     
  390.     r = GetContentRect();
  391.     ViewToGlobal(&r);
  392.     ::InsetRect(&r,1,1);
  393.     (**fList).rView.top = r.top;
  394.     (**fList).rView.left = r.left;
  395. #endif
  396. }
  397.  
  398. /************************************************************************/
  399. /*                                                                        */
  400. /*    My initialization routine                                            */
  401. /*                                                                        */
  402. /************************************************************************/
  403.  
  404. /*    XGStdListBox::Init
  405.  *
  406.  *        Initialization central
  407.  */
  408.  
  409. void XGStdListBox::Init(bool flag)
  410. {
  411.     long l;
  412.     
  413.     l = GetParent()->ReceiveDispatch(KEventGetFont,GetViewID(),(void *)&this);
  414.     fMultiSelect = flag;
  415.     
  416.  
  417. #if OPT_MACOS == 1
  418.     XGDraw draw(this,false);
  419.     Rect r;
  420.     Rect d;
  421.     Point size;
  422.     FontInfo finfo;
  423.     
  424.     /*
  425.      *    Create the control at this location
  426.      */
  427.     
  428.     fFont = GETHIWORD(l);
  429.     fSize = GETLOWORD(l);
  430.     ::TextFont(fFont);
  431.     ::TextSize(fSize);
  432.  
  433.     r = GetContentRect();
  434.     ViewToGlobal(&r);
  435.     r.right -= 15;
  436.     InsetRect(&r,1,1);
  437.     size.h = r.right - r.left;
  438.     GetFontInfo(&finfo);
  439.     size.v = finfo.ascent + finfo.descent + finfo.leading;
  440.     
  441.     d.left = 0;
  442.     d.right = 1;
  443.     d.top = 0;
  444.     d.bottom = 0;
  445.     fList = ::LNew(&r,                            // Display rectangle
  446.                    &d,                            // initial data size
  447.                    size,                        // cell size in pixels
  448.                    0,                            // Use default text proc
  449.                    GetWindow()->_GetGrafPtr(),    // Window owner
  450.                    true,                        // Automatic redrawing
  451.                    false,                        // No grow box
  452.                    false,                        // No horizontal
  453.                    true);                        // Vertical box
  454.     
  455.     if (fList == NULL) {
  456.         throw XPostError("Unable to create list box");
  457.     }
  458.     
  459.     /*
  460.      *    Set list selector flags
  461.      */
  462.     
  463.     if (!fMultiSelect) (**fList).selFlags |= lOnlyOne;
  464. #endif
  465. }
  466.